﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.UI;
using Mes5GService.MES.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mes5GService.MES
{
    [AbpAuthorize]
    public class FileProcessService : AbpZeroTemplateAppServiceBase, IFileProcessService
    {
        private readonly IRepository<FileProcessSpecification, Guid> _fileProcessSpecificationRepository;
        private readonly IRepository<FileProcesses, Guid> _fileProcessesRepository;
        private readonly IRepository<FileWorkStep, Guid> _fileWorkStepRepository;
        public FileProcessService(IRepository<FileProcessSpecification, Guid> fileProcessSpecificationRepository,
            IRepository<FileProcesses, Guid> fileProcessesRepository, IRepository<FileWorkStep, Guid> fileWorkStepRepository)
        {
            this._fileProcessSpecificationRepository = fileProcessSpecificationRepository;
            this._fileProcessesRepository = fileProcessesRepository;
            this._fileWorkStepRepository = fileWorkStepRepository;
        }

        public async Task<ListResultDto<FileProcessSpecificationListDto>> GetAllProcessSpecification()
        {
            var items = ObjectMapper.Map<List<FileProcessSpecificationListDto>>(await _fileProcessSpecificationRepository.GetAll().Include(r=>r.FileTechniqueTask).ToListAsync());
            return new ListResultDto<FileProcessSpecificationListDto>()
            {
                Items = items
            };
        }
        public async Task<FileProcessSpecificationListDto> GetProcessSpecification(Guid id)
        {
            var pf = await _fileProcessSpecificationRepository.GetAll().Include(r => r.FileTechniqueTask).FirstOrDefaultAsync(c => c.Id == id);
            if(pf == null)
            {
                throw new UserFriendlyException("文件不存在");
            }
            var item = ObjectMapper.Map<FileProcessSpecificationListDto>(pf);
            return item;
        }
        public async Task<List<FileProcessMenuDto>> GetProcessFileSpecificationMenu(Guid fileId)
        {
            var menus = new List<FileProcessMenuDto>();
            #region 其它菜单
            var editManagerRoot = new FileProcessMenuDto()
            {
                Selected = false,
                Title= "更改管理",
                Children = null,
                Disabled = false,
                Icon = "profile",
                Id = null,
                Level = 1,
                Open = false,
                Url = "/edit-file-manager"
            };
            menus.Add(editManagerRoot);
            var videoMenu = new FileProcessMenuDto()
            {
                Selected = false,
                Title = "视频查看",
                Children = null,
                Disabled = false,
                Icon = "profile",
                Id = null,
                Level = 1,
                Open = false,
                Url = "/guide-video"
            };
            menus.Add(videoMenu);
            #endregion
            //根据id查找所有工序
            var processes = await _fileProcessesRepository.GetAll().Where(c => c.FileProcessSpecificationId.Equals(fileId)).Include(c => c.FileWorkSteps).ToListAsync();
            //生成菜单
            var root = new FileProcessMenuDto()
            {
                Title = "规程文件",
                Selected = false,
                Children = new List<FileProcessMenuDto>(),
                Disabled = false,
                Icon = "profile",
                Id = fileId,
                Level = 1,
                Open = true
            };
            //文件说明菜单
            var fileInfo = new FileProcessMenuDto()
            {
                Title = "工艺说明",
                Selected = false,
                Children = null,
                Disabled = false,
                Icon = "profile",
                Id = fileId,
                Level = 2,
                Open = false,
                Url = "/file-info"
            };
            root.Children.Add(fileInfo);
            //遍历工序工步
            if (processes.Count > 0)
            {
                foreach(var processIt in processes.OrderBy(c=>c.ProcessesSequence))
                {
                    var processMenu = new FileProcessMenuDto()
                    {
                        Title = $"{processIt.ProcessesSequence}.{processIt.ProcessesName}",
                        Selected = false,
                        Children = new List<FileProcessMenuDto>(),
                        Disabled = false,
                        Id = processIt.Id,
                        Level = 2,
                        Open = false
                    };
                    foreach (var stepIt in processIt.FileWorkSteps.OrderBy(c=>int.Parse(c.WorkStepNumber)))
                    {
                        var stepMenu = new FileProcessMenuDto()
                        {
                            Title = $"工步 {stepIt.WorkStepNumber}",
                            Selected = false,
                            Children = null,
                            Disabled = false,
                            Id = stepIt.Id,
                            Level = 3,
                            Open = false
                        };
                        processMenu.Children.Add(stepMenu);
                    }
                    root.Children.Add(processMenu);
                }
            }
            menus.Add(root);
            return menus;
        }
        public async Task<string> GetProcessFileTechniqueExplain(Guid fileId)
        {
            var pf = await _fileProcessSpecificationRepository.GetAll().Include(r => r.FileTechniqueTask).FirstOrDefaultAsync(c => c.Id == fileId);
            if (pf == null)
            {
                throw new UserFriendlyException("文件不存在");
            }
            return pf.FileTechniqueTask.TechniqueExplain;
        }
        public async Task<FileWorkStepDto> GetProcessFileStepInfo(Guid stepId)
        {
            var step = await _fileWorkStepRepository.GetAll().Where(c => c.Id == stepId)
                .Include(c => c.FileTables)
                .ThenInclude(c => c.FileTableRows)
                .ThenInclude(c => c.FileTableDatas)
                .Include(c => c.FileTables)
                .ThenInclude(c=>c.FileTableStruces)
                .FirstOrDefaultAsync();
            if(step == null)
            {
                throw new UserFriendlyException("工步不存在");
            }
            return ObjectMapper.Map<FileWorkStepDto>(step);
        }
    }
}
